//
//  MSImageView.m
//  MinShengBank
//
//  Created by chenwei on 13-5-7.
//  Copyright (c) 2013年 chen wei. All rights reserved.
//

#import "MSImageView.h"
#define MRScreenWidth      CGRectGetWidth([UIScreen mainScreen].bounds)
#define MRScreenHeight     CGRectGetHeight([UIScreen mainScreen].bounds)

@implementation MSImageView

@synthesize imageView;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.delegate = self;
        self.backgroundColor = [UIColor blackColor];
        self.frame = CGRectMake(0, 0, MRScreenWidth, MRScreenHeight);
        
        //[self initImageView];
        imageView = [[UIImageView alloc]init];
        
        // The imageView can be zoomed largest size
        imageView.frame = CGRectMake(0, (MRScreenHeight-frame.size.height)/2, frame.size.width*3, frame.size.height*3);
        imageView.userInteractionEnabled = YES;
        [self addSubview:imageView];
        
        // Add gesture,double tap zoom imageView.
        UITapGestureRecognizer *doubleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                                           action:@selector(handleDoubleTap:)];
        [doubleTapGesture setNumberOfTapsRequired:1];
        [imageView addGestureRecognizer:doubleTapGesture];
        
        float minimumScale = self.frame.size.width / imageView.frame.size.width;
        [self setMinimumZoomScale:minimumScale];
        [self setZoomScale:minimumScale];
        
    }
    return self;
}

- (void)initImageView
{
    imageView = [[UIImageView alloc]init];
    
        // The imageView can be zoomed largest size
    imageView.frame = CGRectMake(0, 0, self.frame.size.width*3, self.frame.size.height*3);
    imageView.userInteractionEnabled = YES;
    [self addSubview:imageView];
    
        // Add gesture,double tap zoom imageView.
    UITapGestureRecognizer *doubleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                                       action:@selector(handleDoubleTap:)];
    [doubleTapGesture setNumberOfTapsRequired:1];
    [imageView addGestureRecognizer:doubleTapGesture];
    
    float minimumScale = self.frame.size.width / imageView.frame.size.width;
    [self setMinimumZoomScale:minimumScale];
    [self setZoomScale:minimumScale];
}


#pragma mark - Zoom methods

- (void)handleDoubleTap:(UIGestureRecognizer *)gesture
{
    [self removeFromSuperview];
//    float newScale = self.zoomScale * 1.5;
//    CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[gesture locationInView:gesture.view]];
//    [self zoomToRect:zoomRect animated:YES];
}

- (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center
{
    CGRect zoomRect;
    zoomRect.size.height = self.frame.size.height / scale;
    zoomRect.size.width  = self.frame.size.width  / scale;
    zoomRect.origin.x = center.x - (zoomRect.size.width  / 2.0);
    zoomRect.origin.y = center.y - (zoomRect.size.height / 2.0);
    return zoomRect;
}


#pragma mark - UIScrollViewDelegate

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return imageView;
}

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale
{
    CGRect zoomRect = imageView.frame;
    zoomRect.origin.y = (MRScreenHeight-imageView.frame.size.height)/2;
    if (zoomRect.origin.y<20) {
        zoomRect.origin.y = 20;
    }
    imageView.frame = zoomRect;
    [scrollView setZoomScale:scale animated:NO];
}

@end